﻿@{
    ViewBag.Title = "Agent";
    Layout = "~/Views/Shared/_Base.cshtml";
}
@section head{
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")" type="text/javascript"></script>
         <script src="@Url.Content("~/Content/operate/agentmanage.js")" type="text/javascript"></script>
}
<div id="toptoolbar" style="width: 100%">
</div>
<div  class="liger-form" style="padding-top: 2px">
    <table cellpadding="0" cellspacing="0" class="l-table-edit">
        <tr>
           
            <td align="right" class="l-table-edit-td">
                用户名/姓名:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtUserName" type="text" id="txtUserName" ltype="text" />
            </td>
            <td align="right" class="l-table-edit-td">
                身份证:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtUserIDCard" type="text" id="txtUserIDCard" ltype="text" ligerui="width:300" />
            </td>
            <td align="left" class="l-table-edit-td" rowspan="2" valign="bottom">
                <input type="button" value="查询" id="btnSearch" onclick="search()" style="background: url('../../Content/roohui/ligerUI/skins/icons/search2.gif') no-repeat"
                    class="l-button" />
            </td>
        </tr>
        <tr>
           
            <td align="right" class="l-table-edit-td">
                手机:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtHandPhone" type="text" id="txtHandPhone" ltype="text" />
            </td>
            <td align="right" class="l-table-edit-td">
                邮箱:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtEmail" type="text" id="txtEmail" ltype="text" ligerui="width:300" />
            </td>
        </tr>
    </table>
</div>
<div id="membergrid">
</div>
<form action="/" method="post" id="frmAgent" name="frmAgent" style="background-color: white;
display: none">
 <div  class="liger-form" style="padding-top:5px">
    <table cellpadding="0" cellspacing="0" class="l-table-edit" width="100%">
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;用户名:
            </td>
            <td align="left" class="l-table-edit-td" > 
                <input name="txtAgentName" type="text" id="txtAgentName"  validate="{required:true}" />
            </td>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;真实姓名:
            </td>
            <td align="left" class="l-table-edit-td" > 
                <input name="txtRealName" type="text" id="txtRealName"   validate="{required:true}"/>
            </td>
        </tr>
         <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;身份证号:
            </td>
            <td align="left" class="l-table-edit-td" > 
                <input name="txtAgentIDNO" type="text" id="txtAgentIDNO"  validate="{required:true}" />
            </td>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;手机号码:
            </td>
            <td align="left" class="l-table-edit-td" > 
                <input name="txtAgentHandPhone" type="text" id="txtAgentHandPhone"  validate="{required:true}"/>
            </td>
        </tr>
         
         <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;结算方式:
            </td>
            <td align="left" class="l-table-edit-td" > 
               <div id="settlementType" ></div>
            </td>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;结算值:
            </td>
            <td align="left" class="l-table-edit-td" > 
                <input name="txtSettlement" type="text" id="txtSettlement"  validate="{required:true}"/>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;居住地址:
            </td>
            <td align="left" class="l-table-edit-td" colspan="3">
                 <input name="txtAddress" type="text" id="txtAddress" ligerui="width:470" style="width:470"   validate="{required:true}"/>
            </td>
            
        </tr>
         
        <tr>
        <td align="center" colspan="4">
            <span id="lblmsg" class="l-validate-error">&nbsp;</span>
            <input type="hidden" name="isEdit" id="isEdit" value="0" />
        </td>
    </tr>
    </table>
    </div>
</form>
<script type="text/javascript">
    var membergrid;
    var settlementType;
    $(function () {
        $("#toptoolbar").ligerToolBar({ items: [{ text: '新增', click: grid_agent_add, icon: "add" },
            { text: '修改', click: grid_agent_edit, icon: "modify" },
            { text: '删除', click: grid_agent_delete, icon: "delete" },
            { text: '刷新', click: search, icon: "refresh" }
            ]
        });
        membergrid = $("#membergrid").ligerGrid({
            columns: [
            { display: '编码', name: 'AgentCode', width: '5%', isSort: false,
                render: function (item) {
                    return item.AgentInfo.AgentCode;
                }
            },
                {
                    display: '用户名', isAllowHide: false, width: '12%', isSort: false,
                    render: function (row) {

                        var html = '<a href="#" title="点击查看经济人基本信息" onclick="ViewBaseDetail(\'' + row.UserName + '\',\'' + row.RealName + '\')">' + row.UserName + '</a>';

                        return html;
                    }
                },
                { display: '真实姓名', name: 'RealName', width: '10%', isSort: false },
                { display: '身份证号', name: 'IDCardNo', width: '12%', isSort: false },
                { display: '手机', name: 'HandPhone', width: '8%', isSort: false },
                { display: '邮箱', name: 'Email', width: '12%', isSort: false },

                { display: '结算方式', name: 'SettlementType', width: '7%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.AgentInfo.SettlementType) == 0) return '按比例';
                        return '固定值';
                    }
                },
                { display: '结算值/笔', name: 'Settlement', width: '8%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.AgentInfo.SettlementType) == 0) return item.AgentInfo.Settlement + "%";
                        return item.AgentInfo.Settlement;
                    }
                },
                { display: '状态', name: 'Status', width: '5%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.Status) == 1) return '正常';
                        return '禁用';
                    }
                },
                {
                    display: '操作', isAllowHide: false,
                    render: function (row) {

                        var html = '<a href="#" onclick="grid_agent_rowedit(\'' + row.UserName + '\',\'' + row.RealName + '\',\'' + row.HandPhone + '\',\'' + row.IDCardNo + '\',\'' + row.Address + '\',\'' + row.AgentInfo.SettlementType + '\',\'' + row.AgentInfo.Settlement + '\')">修改</a>&nbsp;&nbsp;<a href="#" onclick="agent_delete(\'' + row.UserName + '\',\'' + row.RealName + '\')">删除</a>&nbsp;&nbsp;';
                        html = html + '<a href="#" onclick="ViewDetail(\'' + row.UserName + '\',\'' + row.RealName + '\')">详细信息</a>';
                        if (row.Status == 1) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.UserName + '\',0,\'' + row.RealName + '\')">禁用</a>';
                        }
                        else {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.UserName + '\',1,\'' + row.RealName + '\')">启用</a>';
                        }

                        return html;
                    }
                }
                ],
            width: '100%', height: '100%', rownumbers: true,
            dataAction: 'server',
            url: "/Member/GetAgentByPage",
            pageSizeOptions: [10],
            pageSize: 10
        });

        $.metadata.setType("attr", "validate");
        var v = $("#frmAgent").validate({
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                var mobilereg = /^1[3|5|8]\d{9}$/;
                var idcardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                var emailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,3}$/;
                var settlementreg = /^\d+(\.\d+)?$/;
                if ($('#txtAgentName').val() == "") {
                    $('#txtAgentName').focus();
                    $("#lblmsg").html('用户名不能为空');
                    return;
                }
                if (!emailreg.test($('#txtAgentName').val())) {
                    $('#txtAgentName').focus();
                    $("#lblmsg").html('用户名格式错误');
                    return;
                }
                if ($('#txtRealName').val() == "") {
                    $('#txtRealName').focus();
                    $("#lblmsg").html('真实姓名不能为空');
                    return;
                }
                if ($('#txtAgentIDNO').val() == "") {
                    $('#txtAgentIDNO').focus();
                    $("#lblmsg").html('身份证号码不能为空');
                    return;
                }
                if (!idcardreg.test($('#txtAgentIDNO').val())) {
                    $('#txtAgentIDNO').focus();
                    $("#lblmsg").html('身份证号码格式错误');
                    return;
                }
                if ($('#txtAgentHandPhone').val() == "") {
                    $('#txtAgentHandPhone').focus();
                    $("#lblmsg").html('手机不能为空');
                    return;
                }
                if (!mobilereg.test($('#txtAgentHandPhone').val())) {
                    $('#txtAgentHandPhone').focus();
                    $("#lblmsg").html('手机号码输入错误');
                    return;
                }
                if ($('#txtAddress').val() == "") {
                    $('#txtAddress').focus();
                    $("#lblmsg").html('居住地址不能为空');
                    return;
                }
                if ($('#txtSettlement').val() == "") {
                    $('#txtSettlement').focus();
                    $("#lblmsg").html('结算值不能为空');
                    return;
                }
                if (!settlementreg.test($('#txtSettlement').val())) {
                    $('#txtSettlement').focus();
                    $("#lblmsg").html('结算值输入错误');
                    return;
                }
                var settlementType = liger.get("settlementType").getValue();
                $.ajax({
                    url: "/Member/AgentSave",
                    data: "userName=" + $('#txtAgentName').val() + "&realName=" + $("#txtRealName").val()
                    + "&idCardNo=" + $('#txtAgentIDNO').val() + "&handPhone=" + $("#txtAgentHandPhone").val()
                    + "&address=" + $("#txtAddress").val()
                    + "&optype=" + $("#isEdit").val()
                    + "&settlementType=" + settlementType
                    + "&settlement=" + $("#txtSettlement").val(),
                    type: "post",
                    success: function (result) {
                        //写提交ajax操作
                        if (result == "1") {
                            $.ligerDialog.success("保存成功。");
                            agent_reset();
                            saveDialog.hide();
                            search();
                        }
                        else {
                            if (result == "-1") {
                                $.ligerDialog.error("用户名已存在。");
                            }
                            else if (result == "-2") {
                                $.ligerDialog.error("身份证输入错误，请重新输入。");
                            }
                            else {
                                $.ligerDialog.error("保存失败。");
                            }
                        }

                    }
                });
            }
        });

        // $("#frmAgent").ligerForm();

        settlementType = $("#settlementType").ligerRadioList({
            data: [{ id: 0, name: '按比例' }, { id: 1, name: '固定值'}],
            textField: 'name'
        });
        // settlementType.setValue(1);
    });

    

    function search() {
        membergrid.setOptions(
             {
                 parms:
                [
                    { name: "userName", value: $("#txtUserName").val() },
                    { name: "idCardNo", value: $("#txtUserIDCard").val() },
                    { name: "email", value: $("#txtEmail").val() },
                    { name: "handPhone", value: $("#txtHandPhone").val() },
                    { name: "page", value: 1 }
                ]
             });
        membergrid.loadData();
    }
    function changestate(userName, state, realName) {
        var statestr = "禁用";
        if (state == 1) {
            statestr = "启用";
        }
        $.ligerDialog.confirm('确定【' + statestr + '】经纪人[' + realName + ']吗？', '温馨提示', function (result) {
            if (result) {
                $.ajax({
                    url: '/Member/AgentChangeStatus',
                    data: "userName=" + userName + "&status=" + state,
                    type: "post",
                    success: function (result) {
                        if (result == "1") {
                            //employee.deleteSelectedRow();
                            $.ligerDialog.success(statestr + "经纪人");
                            search();
                        } else {
                            $.ligerDialog.error(statestr + "经纪人失败！", '温馨提示');
                        }
                    }
                });
            }
        });
    }
    
     
</script>